<%@ page import="com.bean.Category" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: 徐洲
  Date: 2023/12/19
  Time: 22:24
  To change this template use File | Settings | File Templates.
--%>
<%
    request.setCharacterEncoding("UTF-8");
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<jsp:useBean id="categoryDao" class="com.daoImpl.CategoryDAOImpl"></jsp:useBean>
<!DOCTYPE html>
<html lang="en">
<%
    request.setCharacterEncoding("utf-8");
    List<Category> categoryList = categoryDao.getCategoryName();
    request.setAttribute("categoryList", categoryList);
%>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>摆烂俱乐部后台</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="robots" content="all,follow">
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome CSS-->
    <link rel="stylesheet" href="../vendor/font-awesome/css/font-awesome.min.css">
    <!-- Google fonts - Roboto -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,700">
    <!-- owl carousel-->
    <link rel="stylesheet" href="../vendor/owl.carousel/assets/owl.carousel.css">
    <link rel="stylesheet" href="../vendor/owl.carousel/assets/owl.theme.default.css">
    <!-- theme stylesheet-->
    <link rel="stylesheet" href="../css/style.default.css" id="theme-stylesheet">
    <!-- Custom stylesheet - for your changes-->
    <link rel="stylesheet" href="../css/custom.css">
    <!-- Favicon-->
    <link rel="shortcut icon" href="../favicon.png">
    <!-- Tweaks for older IEs--><!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script><![endif]-->

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="../assets/lib/layui/css/layui.css" />
    <link rel="stylesheet" href="../assets/css/article/art_list.css" />

    <!-- Favicon-->
    <link rel="shortcut icon" href="../favicon.png">
</head>

<body>
<!-- 卡片区域 -->
<div class="layui-card">
    <div class="layui-card-header">文章分类列表</div>
    <div class="layui-card-body">

        <div style="margin-bottom: 30px">
            <button type="button" data-toggle="modal" data-target="#addC-modal" class="btn btn-primary"><i class="fa fa-sign-in"></i>添加文章分类</button>
        </div>

<%--        添加文章类别--%>
        <div id="addC-modal" tabindex="-1" role="dialog" aria-labelledby="addC-modal" aria-hidden="true" class="modal fade">
            <div class="modal-dialog modal-sm">
                <div class="modal-content">
                    <div class="modal-header">
                        <!-- 添加 -->
                        <h5 class="modal-title">添加分文章类</h5>
                        <button type="button" data-dismiss="modal" aria-label="Close" class="close"><span aria-hidden="true">—</span></button>
                    </div>
                    <div class="modal-body">
                        <form method = "post" action = "./category?action=add">
                            <div class="form-group">
                                <label for="category_name">文章分类名称</label>
                                <input name = "category_name" type="text" required id="category_name" class="form-control">
                            </div>
                            <p class="text-center">
                                <button type="submit" name="submit" class="btn btn-primary"><i class="fa fa-sign-in"></i>添加</button>
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                            </p>
                        </form>
                    </div>
                </div>
            </div>
        </div>

<%--        修改文章类别--%>
        <div id="updateC-modal" tabindex="-1" role="dialog" aria-labelledby="updateC-modal" aria-hidden="true" class="modal fade">
            <div class="modal-dialog modal-sm">
                <div class="modal-content">
                    <div class="modal-header">
                        <!-- 添加 -->
                        <h5 class="modal-title">添加分文章类</h5>
                        <button type="button" data-dismiss="modal" aria-label="Close" class="close"><span aria-hidden="true">—</span></button>
                    </div>
                    <div class="modal-body">
                        <form method = "post" action = "./category?action=update">
                            <div class="form-group">
                                <label for="category_name">文章分类名称</label>
                                <input type="hidden" name="category_id" value="">
                                <input name = "category_name" type="text" required class="form-control" value="">
                            </div>
                            <p class="text-center">
                                <button type="submit" name="submit" class="btn btn-primary"><i class="fa fa-sign-in"></i>修改</button>
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                            </p>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <!-- 列表区域 -->
        <table class="table table-striped table-dark">
            <thead>
            <tr>
                <th scope="col">文章分类ID</th>
                <th scope="col">文章名称</th>
                <th scope="col">操作</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${categoryList}" var="item">
                <tr>
                    <td>${item.category_id}</td>
                    <td>${item.category_name}</td>
                    <td>
<%--                        <button data-category_id="${item.category_id}" type="button" class="btn btn-danger" data-toggle="modal" data-target="#deleteC-modal">删除</button>--%>
    <button data-category_id="${item.category_id}" data-category_name="${item.category_name}" type="button" class="btn btn-primary" data-toggle="modal" data-target="#updateC-modal" onclick="setCategoryIdUp(this)">修改</button>
    <button data-category_id="${item.category_id}" type="button" class="btn btn-danger" data-toggle="modal" data-target="#deleteC-modal" onclick="setCategoryId(this)">删除</button>
                    </td>
                </tr>

            </c:forEach>
            </tbody>
        </table>

<%--        删除文章类别--%>
        <div class="modal fade" id="deleteC-modal" tabindex="-1" role="dialog" aria-labelledby="deleteC-modal" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="deleteUserModalLabel">确认删除</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        确定要删除该文章分类吗？
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                        <form method="post" action="./category?action=delete">
                            <input type="hidden" name="category_id" value="">
                            <button type="submit" class="btn btn-danger">确认删除</button>
                        </form>
<%--                        <button type="button" class="btn btn-danger" onclick="confirmDelete()">确认删除</button>--%>
                    </div>
                </div>
            </div>
        </div>

        <!-- 分页区域 -->
        <div id="pageBox"></div>
    </div>
</div>

<!-- 引入 SweetAlert 库文件 -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<!-- 添加JavaScript代码 -->
<script>
    function setCategoryId(button) {
        // 获取data-category_id属性的值
        var categoryId = button.getAttribute("data-category_id");

        // 将值设置到隐藏字段中
        document.querySelector("#deleteC-modal input[name='category_id']").value = categoryId;
    }

    function setCategoryIdUp(button){
        // 获取data-category_id属性的值
        var categoryId = button.getAttribute("data-category_id");
        var categoryName = button.getAttribute("data-category_name");

        // 将值设置到隐藏字段中
        document.querySelector("#updateC-modal input[name='category_id']").value = categoryId;
        document.querySelector("#updateC-modal input[name='category_name']").value = categoryName;

    }

    // function confirmDelete() {
    //
    //     // 获取隐藏字段的值
    //     var categoryId = document.querySelector("#deleteC-modal input[name='category_id']").value;
    //     alert(categoryId)
    //     if (categoryId.trim() != ""){
    //
    //         window.location = "./category?action=delete&category_id=" + categoryId;
    //     }else {
    //         alert("删除文章失败！")
    //     }
    //
    // }
</script>

<script src="../vendor/jquery/jquery.min.js"></script>
<script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="../vendor/jquery.cookie/jquery.cookie.js"> </script>
<script src="../vendor/owl.carousel/owl.carousel.min.js"></script>
<script src="../vendor/owl.carousel2.thumbs/owl.carousel2.thumbs.js"></script>
<script src="../js/front.js"></script>


<!-- 导入第三方的 JS 插件 -->
<script src="../assets/lib/layui/layui.all.js"></script>
<script src="../assets/lib/jquery.js"></script>
<script src="../assets/lib/template-web.js"></script>
<!-- 导入自己的 JS 脚本 -->
<%--<script src="../assets/js/article/art_list2.js"></script>--%>
</body>

</html>